package com.example.imtoken.modules.private_import

import androidx.lifecycle.MutableLiveData
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.core.http.BaseObserver
import com.example.imtoken.core.logic.network.RetrofitUtils
import com.example.imtoken.entitys.http.KeyStoreEntity
import com.example.imtoken.entitys.http.PrivateKeyEntity
import com.example.imtoken.entitys.http.ResponseEntity
import io.reactivex.schedulers.Schedulers

class PrivateImportCodeViewModel : BaseViewModel() {

    private val keyStoreLiveData = MutableLiveData<KeyStoreEntity>()
    fun getKeyStoreData(): MutableLiveData<KeyStoreEntity> {
        return keyStoreLiveData
    }

    private val privateKeyLiveData = MutableLiveData<PrivateKeyEntity>()
    fun getPrivateKeyData(): MutableLiveData<PrivateKeyEntity> {
        return privateKeyLiveData
    }

    fun getPrivateKeyData(password: String?) {
        RetrofitUtils.apiService?.walletPrivateKey(password)
            ?.subscribeOn(Schedulers.io())
            ?.unsubscribeOn(Schedulers.io())
            ?.subscribe(object : BaseObserver<ResponseEntity<PrivateKeyEntity>>() {
                override fun onSuccess(value: ResponseEntity<PrivateKeyEntity>) {
                    if (value.code == 0) {
                        privateKeyLiveData.postValue(value.data)
                    } else {
                        mErrorStr.postValue(value.msg)
                    }
                }

                override fun onFailed(e: Throwable) {
                }

            })
    }

    fun getKeyStoreData(password: String?) {
        RetrofitUtils.apiService?.walletKeyStore(password)
            ?.subscribeOn(Schedulers.io())
            ?.unsubscribeOn(Schedulers.io())
            ?.subscribe(object : BaseObserver<ResponseEntity<KeyStoreEntity>>() {
                override fun onSuccess(value: ResponseEntity<KeyStoreEntity>) {
                    if (value.code == 0) {
                        keyStoreLiveData.postValue(value.data)
                    } else {
                        mErrorStr.postValue(value.msg)
                    }
                }

                override fun onFailed(e: Throwable) {
                }

            })
    }

}